import zzd from 'gdt-jsapi'
import OrgCtrl from '../ts/controller'

export const singleSignOn = () => {
  zzd.getAuthCode({}).then(result => {
    if (result) {
      // alert(result.code)
      uni.request({
        url: 'http://zydd.orginone.org.cn/ZZDLogin.in',
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        data: {
          decryptData: result.code || ''
        },
        success: async res => {
          // zzd.alert({
          //   message: JSON.stringify(res)
          // })
          if (res.data.success && res.data.code === 200) {
            sessionStorage.setItem('accessToken', res.data.data)
            await OrgCtrl.auth.noSignIn(res.data.data)
            uni.reLaunch({
              url: '/pages/home/home'
            })
          } else {
            /** 免登认证失败，前往登录页 */
            uni.reLaunch({
              url: '/pages/auth/login?clear=1'
            })
          }
        },
        fail: _err => {
          /** 免登认证失败，前往登录页 */
          uni.reLaunch({
            url: '/pages/auth/login?clear=1'
          })
        }
      })
    }
  })
}
